#include "StatusBar.h"

#include "../Logger.h"
#include "../Game.h"

CGUIStatusBar::CGUIStatusBar(const irr::core::rect<s32>& area, irr::gui::IGUIEnvironment* environ, s32 width = 0, s32 height = 0) : 
	IGUIElement(irr::gui::EGUIET_ELEMENT, environ, environ->getRootGUIElement(), -1, area),
	m_color(video::SColor(255,255,255,255)), m_percent(100), m_width(width), m_height(height)
	{
	if(m_width <= 0 || m_height <= 0) {
		m_width = area.LowerRightCorner.X - area.UpperLeftCorner.X;
		m_height = area.LowerRightCorner.Y - area.UpperLeftCorner.Y;
	}
	m_text = core::stringw(m_percent);
}

CGUIStatusBar::~CGUIStatusBar() {
}

void CGUIStatusBar::draw() {	
	irr::gui::IGUISkin* skin = Environment->getSkin();
	irr::core::dimension2di dim = Environment->getVideoDriver()->getScreenSize();
	if(getAbsolutePosition().UpperLeftCorner.X > -m_width &&
			getAbsolutePosition().UpperLeftCorner.Y > -m_height &&
			getAbsolutePosition().LowerRightCorner.X < dim.Width + m_width &&
			getAbsolutePosition().LowerRightCorner.Y < dim.Height + m_height) {
		irr::core::rect<s32> tmprect = AbsoluteRect;
		tmprect.LowerRightCorner.X = (tmprect.UpperLeftCorner.X + (tmprect.LowerRightCorner.X - tmprect.UpperLeftCorner.X)*m_percent);
		skin->draw2DRectangle(static_cast<gui::IGUIElement*>(this),m_color,AbsoluteRect,&AbsoluteClippingRect);
//		irr::core::stringw tmpst = core::stringw(getAbsolutePosition().UpperLeftCorner.X) + irr::core::stringw(L",") +
//		core::stringw(getAbsolutePosition().UpperLeftCorner.Y);
//		Facets::Logger::Instance()->log(L"CGUIStatusBar",tmpst.c_str(),irr::ELL_INFORMATION);
		// Draw text in center
		skin->getFont(irr::gui::EGDF_DEFAULT)->draw(m_text.c_str(), AbsoluteRect, skin->getColor(gui::EGDC_BUTTON_TEXT), true, true, &AbsoluteClippingRect);	
	}
}

void CGUIStatusBar::setColor(const video::SColor& col) {
    m_color = col;
}

const video::SColor& CGUIStatusBar::getColor() const
{
    return m_color;
}

void CGUIStatusBar::setPercent(float per) {
	m_percent = per;
	m_text = core::stringw(m_percent);
}

const float& CGUIStatusBar::getPercent() const {
	return m_percent;
}

void CGUIStatusBar::updateLocation(const core::position2di& pos) {
	core::position2di corners[2] = { pos, pos };
	corners[0].X -= m_width/2; corners[0].Y -= m_height/2;
	corners[1].X += m_width/2; corners[1].Y += m_height/2;
	setRelativePosition(core::rect<s32>(corners[0],corners[1]));
}

void StatusBar::CreateStatusBar() {
	core::vector3df temp = m_parent->getAbsolutePosition();
	temp.Y+=m_offset;
	irr::core::position2di temppos = m_sceneManager->getSceneCollisionManager()->getScreenCoordinatesFrom3DPosition(temp);
	core::position2di corners[2] = { temppos, temppos };
	corners[0].X -= m_width/2; corners[0].Y -= m_height/2;
	corners[1].X += m_width/2; corners[1].Y += m_height/2;
	m_bar = new CGUIStatusBar(core::rect<s32>(corners[0],corners[1]),m_sceneManager->getGUIEnvironment());
	m_bar->drop();
}

StatusBar::StatusBar(irr::scene::ISceneNode* parent, float offset, irr::scene::ISceneManager* scn) :
	ContainedNode(parent,offset,150,5,scn),
	m_percent(100), m_barColor(255,0,0,255) {
	CreateStatusBar();
//	m_barNode = m_sceneManager->addBillboardSceneNode(m_parent,irr::core::dimension2df(m_width,m_height),
//			getLocalPosition(),-1,m_barColor,m_barColor);
}

StatusBar::StatusBar(irr::scene::ISceneNode* parent, float offset, const irr::video::SColor& col, irr::scene::ISceneManager* scn) : 
	ContainedNode(parent,offset,150,5,scn),
	m_percent(100), m_barColor(col) { 
	CreateStatusBar();
//	m_barNode = m_sceneManager->addBillboardSceneNode(m_parent,irr::core::dimension2df(m_width,m_height),
//			getLocalPosition(),-1,m_barColor,m_barColor);
}

StatusBar::StatusBar(irr::scene::ISceneNode* parent, float offset, const irr::video::SColor& col, float width, float height, irr::scene::ISceneManager* scn) :
	ContainedNode(parent,offset,width,height,scn),
	m_percent(100), m_barColor(col) { 
	CreateStatusBar();
//	m_barNode = m_sceneManager->addBillboardSceneNode(m_parent,irr::core::dimension2df(m_width,m_height),
//			getLocalPosition(),-1,m_barColor,m_barColor);
}		
